class MinStack2 {
    private stack: number[];
    private minvalue: number = 0;



    constructor() {
        this.stack = [];
    }

    push(x: number): void {

        if (this.stack.length === 0) {
            this.minvalue = x;
        }

        this.stack.push(x - this.minvalue);

        if (x < this.minvalue) {
            this.minvalue = x;
        }
    }

    pop(): void {
        let key: number | undefined = this.stack.pop();
        if (key != null && key < 0) {
            this.minvalue += -key;
        }
    }

    top(): number {
        let result: number = this.stack[this.stack.length - 1];
        if (result <= 0) {
            return this.minvalue;
        }

        return result + this.minvalue;

    }

    min(): number {
        return this.minvalue;

    }
}

// var obj: MinStack2 = new MinStack2()
// obj.push(1)
// obj.push(3)
// obj.push(2)
// obj.push(4)

// obj.pop()
// var param_3 = obj.top()
// var param_4 = obj.min()


// console.log(param_3);
// console.log(param_4);

/**
 * Your MinStack object will be instantiated and called as such:
 * var obj = new MinStack()
 * obj.push(x)
 * obj.pop()
 * var param_3 = obj.top()
 * var param_4 = obj.min()
 */